package P150;

import java.util.HashSet;
import java.util.Set;

/**
 * @author LiangMu
 * @version \$Id: P32.java, v 0.1, 2024年07月05日 22:58 LiangMu Exp $$
 */
public class P32 {

    public boolean isValidSudoku(char[][] board) {
        int[][] rows = new int[9][9];
        int[][] columns = new int[9][9];
        int[][][] subboxes = new int[3][3][9];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char c = board[i][j];
                if (c != '.') {
                    int index = c - '0' - 1;
                    rows[i][index]++;
                    columns[j][index]++;
                    subboxes[i / 3][j / 3][index]++;
                    if (rows[i][index] > 1 || columns[j][index] > 1 || subboxes[i / 3][j / 3][index] > 1) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public boolean isValidSudoku2(char[][] board) {
        int[][] x = new int[9][9];
        int[][] y = new int[9][9];
        int[][][] z = new int[3][3][9];
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] != '.') {
                    int index = board[i][j] - '0' - 1;
                    x[i][index]++;
                    y[j][index]++;
                    z[i / 3][j / 3][index]++;

                    if (x[i][index] > 1 || y[j][index] > 1 || z[i / 3][j / 3][index] > 1) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

}
